'use strict';

module.exports = () => async (ctx, next) => {
  const request = `${ctx.path} query${JSON.stringify(ctx.query)} body ${JSON.stringify(ctx.request.body)} user ${JSON.stringify(ctx.session ? ctx.session.user : '{}')}`;
  await next();
  let body;
  if (!/(^\/static\/|^\/docs\/)/.test(ctx.path)) { // 请求静态资源不进行记录
    try {
      body = JSON.stringify(ctx.body).slice(0, 150);
    } catch (err) {
      body = 'stream';
    }
    const response = `statusCode ${ctx.response.status} body ${body}`;
    ctx.logger.info(JSON.stringify({ request, response }));
  }
};
